package com.example.fangyimanagement;

import com.example.fangyimanagement.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/userManage")
public class UserManageController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("/addUser")
    public Result<Void> addUser(@RequestBody Map<String, String> param) {
        String account = param.get("account");
        String name = param.get("name");
        String role = param.get("role");
        String org = param.get("org");
        String remark = param.get("remark");
        // 先查询库里有没有这个账号，只有账号要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_user WHERE account=?", account);
        if (!count.isEmpty()) {
            return Result.failed("和其他用户的账号重复，请修改账号~");
        }
        //        操作数据库
        String sql = "insert into glpt_user (account, name, password, role_code, org_code, create_time, remark) " +
                "values (?, ?, '123456', ?, ?, CURRENT_TIME, ?)";
        int rows = this.jdbcTemplate.update(sql, account, name, role, org, remark);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/deleteUser")
    public Result<Void> deleteUser(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        //        操作数据库
        int rows = this.jdbcTemplate.update("delete from glpt_user where id =?", id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("没找到这个账号");
        }
    }
    @RequestMapping("/updateUser")
    public Result<Void> updateUser(@RequestBody Map<String, String> param) {
        String id = param.get("id");
        String account = param.get("account");
        String name = param.get("name");
        String role = param.get("role");
        String org = param.get("org");
        String remark = param.get("remark");
        // 先查询库里有没有这个账号，只有账号要做防重复的处理
        List<Map<String, Object>> count = this.jdbcTemplate.queryForList("SELECT * FROM glpt_user WHERE account=? AND id!=?", account, id);
        if (!count.isEmpty()) {
            return Result.failed("和其他用户的账号重复，请修改账号~");
        }
        //        操作数据库
        String sql = "update glpt_user set account=?, name=?, role_code=?, org_code=?, remark=? where id =?";
        int rows = this.jdbcTemplate.update(sql, account, name, role, org, remark, id);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.failed("添加失败");
        }
    }
    @RequestMapping("/userList")
    public Result<List<Map<String, Object>>> userList(@RequestBody Map<String, String> param) {
        int page = Integer.parseInt(param.get("page"));
        int size = Integer.parseInt(param.get("size"));
        String account = param.get("account");
        String name = param.get("name");
        String roleCode = param.get("role");
        String orgCode = param.get("org");

        List<Object> paramList = new ArrayList<>();
        String sql = "SELECT \n" +
                "    glpt_user.id, account, name, " +
                "glpt_user.role_code AS roleCode, \n" +
                "    glpt_role.role_name AS roleName, " +
                "glpt_user.org_code AS orgCode, glpt_org.org_name AS orgName," + //
                "glpt_user.remark, glpt_user.create_time AS createTime \n" +
                "FROM glpt_user\n" +
                "    " +
                "JOIN glpt_role ON glpt_user.role_code = glpt_role.role_code\n" +
                "    " +
                "JOIN glpt_org ON glpt_user.org_code = glpt_org.org_code " +
                "WHERE name LIKE '%" + name + "%' " +
                "AND account LIKE '%" + account + "%'";
        if (roleCode != null && !roleCode.isEmpty()) {
            sql = sql + " AND glpt_user.role_code = ?";
            paramList.add(roleCode);
        }
        if (orgCode != null && !orgCode.isEmpty()) {
            sql = sql + " AND glpt_user.org_code = ?";
            paramList.add(orgCode);
        }

        String countSql = "SELECT COUNT(1) FROM ("+ sql +") t";
        Integer count = this.jdbcTemplate.queryForObject(countSql, Integer.class, paramList.toArray());

        String querySql = sql + " ORDER BY glpt_user.id LIMIT ?, ?";
        paramList.add((page - 1) * size);
        paramList.add(size);
        List<Map<String, Object>> userList = this.jdbcTemplate.queryForList(querySql, paramList.toArray());

        return Result.success(userList).setTotal(count);
    }
}
